package com.codechuan.demo.ui.addMinAccount;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.TokenObject;
import com.codechuan.demo.ui.myToken.MyToken;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;

public class AddMinAccountActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add_min_account);

        // 发送验证码
        Button sendCode = findViewById(R.id.sendCode);
        sendCode.setOnClickListener(this);

        // 获取Token
        Button getToken = findViewById(R.id.getToken);
        getToken.setOnClickListener(this);

        // 返回列表
        Button add_return = findViewById(R.id.add_return);
        add_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (R.id.sendCode == id) {
            // 判断小号是否大于1个
            TextView add_msg = findViewById(R.id.add_msg);
            add_msg.setText(Constant.CODE_MSG_Z);
            // 获取手机号
            TextView phone = findViewById(R.id.phone);
            new Thread(() -> {
                Constant.CODE_MSG = HttpControllerUtil.getWeChatMiniProgramCode(phone.getText().toString());
                Constant.handler.post(() -> {
                    add_msg.setText(Constant.CODE_MSG);
                });
            }).start();

        }
        if (R.id.getToken == id) {
            // 获取手机号
            TextView phone = findViewById(R.id.phone);
            String phoneString = phone.getText().toString();
            // 获取验证码
            TextView core = findViewById(R.id.core);
            String coreString = core.getText().toString();
            // 消息提示
            TextView add_msg = findViewById(R.id.add_msg);
            Constant.executorService.execute(() -> {
                JSONObject weChatMiniProgramToken = HttpControllerUtil.getWeChatMiniProgramToken(phoneString, coreString);
                Constant.handler.post(() -> {
                    add_msg.setText(Constant.TOKEN_TQ);
                });
                // 提示消息
                if (weChatMiniProgramToken == null) {
                    Constant.handler.post(() -> {
                        add_msg.setText(weChatMiniProgramToken.getString("提取失败."));
                    });
                } else {
                    Constant.handler.post(() -> {
                        add_msg.setText(Constant.TOKEN_TQ_SUCCESS);
                    });
                    String ticket = weChatMiniProgramToken.getString("ticket");
                    String mobile = weChatMiniProgramToken.getString("mobile");
                    String token = weChatMiniProgramToken.getString("token");
                    TokenObject tokenObject = new TokenObject(token, ticket, mobile, "正常", 0, "1", Constant.USER_ACCOUNT);
                    Constant.SON_TOKENS.add(tokenObject);
                    // 插入数据库
                    boolean b = MyHttpUtil.insertUserToken(tokenObject);
                    if (b) {
                        Constant.handler.post(() -> {
                            add_msg.setText(Constant.TOKEN_TQ_SUCCESS);
                            DialogUtil.showDialog(Constant.context, "添加小号成功");
                        });
                    } else {
                        add_msg.setText("添加小号失败");
                        DialogUtil.showDialog(Constant.context, "添加小号失败");
                    }
                }
            });
        }
        if (R.id.add_return == id) {
            Intent intent = new Intent();
            intent.setClass(Constant.context, MyToken.class);
            startActivity(intent);
        }
    }
}